
var num = 1;
var pageSize = 10;

var dataInfo={
    "eventType":"",
    "username":"",
    "fromTime":"",
    "toTime":"",
    "timeSort":"",
    "usernameSort":"",
    "eventTypeSort":"",
    "typeSort":"",
    "click":"false"

};

function getCookie(name){
    var strcookie = document.cookie;//获取cookie字符串
    var arrcookie = strcookie.split("; ");//分割
    //遍历匹配
    for ( var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == name){
            return arr[1];
        }
    }
    return "";
}

/**
 * 查询日志记录接口
 * @param pageNum
 * @param pageSize
 * @param callback
 */

function listLogs(pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo?pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("请求日志列表错误！");
        },
        dataType: "json",
    });
}

function saveNormalLog() {
    isOnline("auditor");
    var uname=getSessionStorage("username");
    window.location.href=HOST.concat("/storesLog?username="+uname + "&token=" + config.token+"&type=normal");
}

function listLogsByCond(eventType,username, from, to, pageNum, pageSize,timeSort,usernameSort,typeSort){
    isOnline("auditor");
    $.ajax({
        url: HOST.concat("/logsInfo?username="+username+"&eventType="+dataInfo.eventType+"&click="+dataInfo.click+"&eventTypeSort="+dataInfo.eventTypeSort+"&typeSort="+typeSort+"&timeSort="+timeSort+"&usernameSort="+usernameSort+"&fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
            checkLogin(datas);
            //console.log(datas);
            addContent(datas);
        },
        error: function(){
            console.log("请求日志列表错误！");
        },
        dataType: "json",
    });
}
/**
 * 按时间区间查询日志接口
 * @param from
 * @param to
 * @param pageNum
 * @param pageSize
 */
function listLogsByTimerange(from, to, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/timeRange?fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据时间区间请求日志列表错误！");
        },
        dataType: "json",
    });
}

/**
 * 按用户名查询日志接口
 * @param username
 * @param pageNum
 * @param pageSize
 */
function listLogsByUser(username, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/username?username="+ username +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据用户名请求日志列表错误！");
        },
        dataType: "json",
    });
}

/**
 * 按用户名和时间区间查询日志接口
 * @param from
 * @param to
 * @param pageNum
 * @param pageSize
 */
function listLogsByUserAndTimerange(username, from, to, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/username/timeRange?username="+username+"&fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据用户名和时间区间请求日志列表错误！");
        },
        dataType: "json",
    });
}
function isNull( str ){
    if ( str == "" ) return true;
    // if (str == undefined) return true;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str);
}


function getLogsInfoList(num){

            listLogsByCond(dataInfo.eventType,dataInfo.username, dataInfo.fromTime , dataInfo.toTime, num, pageSize,dataInfo.timeSort,dataInfo.usernameSort,dataInfo.typeSort);

}


function addContent(res){
        //查询成功
        //console.log(res);
        if(res.meta.state==="success") {
            var list = res.data.resArray;
				//console.log("有查询记录");
				$("#tbody").empty();
				$("#pageNum").text(num);
                $("#numValue").text(res.data.resCount);
				for (var p in list) {
				    var item = list[p];//单条数据
				    var createTime = item["createTime"];
				    var username = item["username"];
				    var operateInfo = item["operateInfo"];
				    //console.log(operateInfo);
				    var result = item["result"];
				    var type=item["type"];
				    var eventType=item["eventType"];

				    if(eventType==0){
				        eventType="业务级事件";
                    }else if(eventType==1){
				        eventType="系统级事件";
                    }else{
				        eventType="其他事件";
                    }

				    if(result=="success"){
				        result="<p style='color: green;'>"+result+"</p>";
                    }else {
                        result="<p style='color: red;'>"+result+"</p>";
                    }

				    $("#tbody").append("<div class=\"op-data tr clearfix border-bottom-none\">" +
                        "                        <div class=\"td w20\">" + type + "</div>" +
				        "                        <div class=\"td w15\">" + username + "</div>" +
                        "                        <div class=\"td w15\">" + eventType+ "</div>" +
				        "                        <div class=\"td w20\">" + operateInfo + "</div>" +
                        "                        <div class=\"td w15\">" + createTime + "</div>" +
				        "                        <div class=\"td w15\">" + result + "</div>" +
				        "               </div>");
				}
            
        }
        //查询失败
        else if(res.meta.state=="failed"){
            if(num==1){
                $("#tbody").empty();
                $("#numValue").text(0);
                //alert("无查询结果!");
            }else {
                alert("已经是最后一页!");
                num--;
            }
        }
}
function logsLastPage(){
    dataInfo.click="false";
	if(num<=1){
		num = 1;
		alert("已经是第一页!");
		$("#pageNum").text(num);
	}else{
		num--;
		getLogsInfoList(num);
	}
}
function logsNextPage(){
       dataInfo.click="false";
		num++;
		getLogsInfoList(num);
}
function logsFirstPage(){
    dataInfo.click="false";
	num = 1;
	$("#pageNum").text(num);
	getLogsInfoList(num);
}
$("#search").click(function(){
    dataInfo.click="true";
	num = 1;
	$("#pageNum").text(num);
	dataInfo.eventType=$("#eventType").val();
	dataInfo.username=$("#username").val();
	dataInfo.fromTime=$("#from").val();
	dataInfo.toTime=$("#to").val();
	//默认都是按照时间降序排的
	dataInfo.timeSort="";
	dataInfo.usernameSort="";
	dataInfo.typeSort="";
	dataInfo.eventTypeSort="";

    if( dataInfo.username.trim()!="" && !checkLogUsername(dataInfo.username)){
        alert("用户名不能超过16位，且不包含除出下划线和减号以外的特殊字符");
        $("#username").val("");
        return;
    }

    if(!isNull(dataInfo.fromTime)&&!isNull(dataInfo.toTime)) {
        if (compareFromAndTo(dataInfo.fromTime ,dataInfo.toTime)){
            dataInfo.fromTime = dataInfo.fromTime .replace(/-/g, "/") + " 00:00:00";
            dataInfo.toTime = dataInfo.toTime.replace(/-/g, "/") + " 23:59:59";
      }else {
            alert("时间区间先后存在问题!");
            return;
        }
    }else if(!isNull(dataInfo.fromTime )){
        dataInfo.fromTime  = dataInfo.fromTime .replace(/-/g, "/") + " 00:00:00";
  }else if(!isNull(dataInfo.toTime)){
        dataInfo.toTime = dataInfo.toTime.replace(/-/g, "/") + " 23:59:59";
   }
    
    getLogsInfoList(num);
});
getLogsInfoList(1);

//时间升序
$("#timeAesc").live("click",function () {
   dataInfo.timeSort="0";
   dataInfo.usernameSort="";
   dataInfo.typeSort="";
   dataInfo.eventTypeSort="";
   dataInfo.click="false";
   getLogsInfoList(num);
});
//时间降序
$("#timeDesc").live("click",function () {
    dataInfo.timeSort="1";
    dataInfo.usernameSort="";
    dataInfo.typeSort="";
    dataInfo.eventTypeSort="";
    dataInfo.click="false";
    getLogsInfoList(num);
});
//用户名升序
$("#usernameAesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="0";
    dataInfo.typeSort="";
    dataInfo.eventTypeSort="";
    dataInfo.click="false";
    getLogsInfoList(num);
});
//用户名降序
$("#usernameDesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="1";
    dataInfo.typeSort="";
    dataInfo.eventTypeSort="";
    dataInfo.click="false";
    getLogsInfoList(num);
});
//类型升序
$("#typeAesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="";
    dataInfo.typeSort="0";
    dataInfo.eventTypeSort="";
    dataInfo.click="false";
    getLogsInfoList(num);
});
//操作类型降序
$("#typeDesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="";
    dataInfo.typeSort="1";
    dataInfo.eventTypeSort="";
    dataInfo.click="false";
    getLogsInfoList(num);
});
//事件类型排序
$("#eventTypeAesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="";
    dataInfo.typeSort="";
    dataInfo.eventTypeSort="0";
    dataInfo.click="false";
    getLogsInfoList(num);
});
$("#eventTypeDesc").live("click",function () {
    dataInfo.timeSort="";
    dataInfo.usernameSort="";
    dataInfo.typeSort="";
    dataInfo.eventTypeSort="1";
    dataInfo.click="false";
    getLogsInfoList(num);
});

/**********************统计信息*******************/

$("#statisticalBtn").die().live("click",function () {
    isOnline("auditor");
    //准备容器
    $("#myFooter").remove();
    $("#contentArea").empty();
    //添加hash标记
    window.location.hash="#statistical";
    $.get("statisticalInfo.html",function(data){
        $("#contentArea").html(data);
    });

});



